package business.transcation;

import org.apache.ibatis.session.SqlSession;

import business.domain.mapper.AlarmUserMapper;
import core.query.Pagination;
import core.query.QueryParam;
import core.transcation.BaseMapper;
import core.transcation.ICommand;
import core.transcation.IContext;
import core.transcation.TranscationCallback;
import core.transcation.TranscationTemplate;
import core.util.BeanUtils;

public class AlarmUserQueryCommand implements ICommand {

//	private ExceptionHander exceptionHander;

	public AlarmUserQueryCommand() {
//		exceptionHander = new ExceptionHander() {
//
//			@Override
//			public void handleException(ICommandContext context, Exception e) {
//				e.printStackTrace();
//			}
//		};
	}

	@Override
	public Object execute(final IContext context) {
		return new TranscationTemplate().execute(new TranscationCallback() {

			@SuppressWarnings("rawtypes")
			@Override
			public Object doInSession(SqlSession session) {
				BaseMapper mapper = session.getMapper(AlarmUserMapper.class);
				QueryParam queryContext = (QueryParam) context;
				mapper.query(context);
				queryContext.setResult(mapper.query(context));
//				queryContext.putQueryResult(mapper.query(context));
				if (queryContext.getPagination() != null) {
					Pagination pageList2 = Pagination
							.newPaginationScope(queryContext.getPagination());
					QueryParam m;
					m = BeanUtils.clone(queryContext);
					m.setPagination(pageList2);
					queryContext.getPagination().setLastPage(mapper.count(m));
				}
				return queryContext;
			}
		});
	}

}
